﻿using System;
using System.Web;
using YiCheap.Service.Localization;

namespace YiCheap.Web.Framework.ViewEngines
{
    public delegate LocalizedString Localizer(string key);

    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {
        private readonly ILocalizationService _localizationService;

        protected WebViewPage(ILocalizationService localizationService)
        {
            _localizationService = localizationService;
        }

        private Localizer _localizer;

        public Localizer T
        {
            get
            {
                if (_localizer == null)
                {
                    _localizer = (key) =>
                        {
                            var result = _localizationService.GetResource(key);

                            return new LocalizedString(result);
                        };

                }
                    return _localizer;

            }
        }
    }

    public class LocalizedString : MarshalByRefObject, IHtmlString
    {
        private readonly string _localized;

        public LocalizedString(string localized)
        {
            _localized = localized;
        }

        public string ToHtmlString()
        {
            return _localized;
        }
    }

    public abstract class WebViewPage : WebViewPage<dynamic>
    {
        protected WebViewPage(ILocalizationService localizationService) : base(localizationService)
        {
        }
    }
}